/*  SSNewDocController.m
 *  MESLib
 * 
 * Copyright 2003-2009 Starline Software Ltd
 * 
 * This file is part of MESLib.
 * 
 * MESLib is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * MESLib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with MESLib. If not, see <http://www.gnu.org/licenses/>.
 */
//  Created by Tobias Haughton on Mon Nov 24 2003.
//

#import "SSNewDocController.h"
#import "SSCatalystDocument.h"

@implementation SSNewDocController

+ (id)sharedNewDocController {
   static SSNewDocController *sharedNewDocController = nil;

   if(!sharedNewDocController) {
      sharedNewDocController = [[SSNewDocController allocWithZone:[self zone]] init];
   }
   return sharedNewDocController;
}

- (id)init
{
   if (self = [super init]) {
   }
   return self;
}

- (void)initiateNewDocument
{
   NSOpenPanel *openPanel = [NSOpenPanel openPanel];
   int result;
   NSString *path;
   NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
   
   [openPanel setTitle:@"Select project folder for new file"];
   [openPanel setPrompt:@"Select"];
   [openPanel setAllowsMultipleSelection:NO];
   [openPanel setCanChooseDirectories:YES];
   [openPanel setCanChooseFiles:NO];
   [openPanel setCanCreateDirectories:YES];

   result = [openPanel runModalForDirectory:[defaults objectForKey:SSDefaultProjectFolderKey] file:@""];
   
   if(result == NSOKButton) {
      //user has selected a folder so open a new file
      path = [openPanel filename];
      id theDocument = [[NSDocumentController sharedDocumentController] openUntitledDocumentOfType:@"Catalyst Document" display:YES];
      [theDocument setProjectFolder:path];
      [defaults setObject:path forKey:SSDefaultProjectFolderKey];
   }
}


@end
